package com.inossem.allotin.adapter.input;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.inossem.allotin.R;
import com.inossem.wms.model.dic.DicWhStorageBin;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by 34221 on 2019/8/28.
 * 目标仓位适配器
 */
public class ShipOutBinListAdapter extends RecyclerView.Adapter<ShipOutBinListAdapter.ViewHolder> {

    private Context context;
    private List<DicWhStorageBin> list;
    private String binCode;

    public ShipOutBinListAdapter(Context context, List<DicWhStorageBin> list, String binCode) {
        this.context = context;
        this.list = list;
        this.binCode = binCode;
    }

    public DicWhStorageBin getCheckBean() {
        for (DicWhStorageBin vo : list) {
            if (!TextUtils.isEmpty(binCode) && vo.getBinCode().equals(binCode)) {
                return vo;
            }
        }
        return null;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_ship_out_choose_type_bin_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        DicWhStorageBin vo = list.get(i);
        if (!TextUtils.isEmpty(binCode) && vo.getBinCode().equals(binCode)) {
            viewHolder.tv.setBackgroundResource(R.color.mat_ok);
        } else if (i % 2 == 0) {
            viewHolder.tv.setBackgroundResource(R.color.base_white);
        } else {
            viewHolder.tv.setBackgroundResource(R.color.base_bg);
        }

        viewHolder.tv.setText(vo.getBinCode());
        viewHolder.tv.setOnClickListener(v -> {
            binCode = vo.getBinCode();
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv)
        TextView tv;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
